// MyFamily - XML based genealogy program.
// Copyright (C) 2006  Doug Swisher
// See the file COPYING.txt for license details.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

using DataModel;


namespace WebLib
{
    class WebPage
    {
        protected static void WriteHeader(HtmlTextWriter writer, string title)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Html);
            writer.RenderBeginTag(HtmlTextWriterTag.Head);

            writer.RenderBeginTag(HtmlTextWriterTag.Title);
            writer.Write(title);
            writer.RenderEndTag();

            // TODO - Doug - 8/5/2006 - write out some meta tags for search engine consumption

            writer.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
            writer.AddAttribute(HtmlTextWriterAttribute.Href, "style.css");
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
            writer.AddAttribute("media", "all");
            writer.RenderBeginTag(HtmlTextWriterTag.Link);
            writer.RenderEndTag();

            writer.AddAttribute(HtmlTextWriterAttribute.Src, "tabber.js");
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");
            writer.RenderBeginTag(HtmlTextWriterTag.Script);
            writer.RenderEndTag();

            // TODO - Doug - 8/5/2006 - include a print variant of the CSS

            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Body);
        }

        protected static void CreateHomeTab(HtmlTextWriter writer, FileNameBuilder namer, FamilyInfo info)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabbertab");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.RenderBeginTag(HtmlTextWriterTag.H3);
                    writer.Write("Home");
                writer.RenderEndTag();
                writer.RenderBeginTag(HtmlTextWriterTag.P);
                    writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                        writer.RenderBeginTag(HtmlTextWriterTag.Li);
                            WritePageLink(writer, namer, FileNameBuilder.Page.IndividualIndex);
                        writer.RenderEndTag();

                        writer.RenderBeginTag(HtmlTextWriterTag.Li);
                            WritePageLink(writer, namer, FileNameBuilder.Page.SourceIndex);
                        writer.RenderEndTag();
                    writer.RenderEndTag();
                writer.RenderEndTag();
            writer.RenderEndTag();
        }

        protected static void CreateSourceTab(HtmlTextWriter writer, FileNameBuilder namer, FamilyInfo info)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabbertab");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.RenderBeginTag(HtmlTextWriterTag.H3);
                    writer.Write("Sources");
                writer.RenderEndTag();
                writer.RenderBeginTag(HtmlTextWriterTag.P);
                    writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                    List<FamilySource> list1 = new List<FamilySource>();
                    list1.AddRange(info.Sources.Values);

                    foreach (FamilySource source in list1)
                    {
                        writer.RenderBeginTag(HtmlTextWriterTag.Li);
                            WriteSourceLink(writer, namer, source);
                        writer.RenderEndTag();
                    }

                    writer.RenderEndTag();
                writer.RenderEndTag();
            writer.RenderEndTag();
        }

        protected static void CreateIndividualsTab(HtmlTextWriter writer, FileNameBuilder namer, FamilyInfo info)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabbertab");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.RenderBeginTag(HtmlTextWriterTag.H3);
                    writer.Write("Individuals");
                writer.RenderEndTag();
                writer.RenderBeginTag(HtmlTextWriterTag.P);
                    writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                        List<FamilyIndividual> list = new List<FamilyIndividual>();
                        list.AddRange(info.Individuals.Values);
                        list.Sort();

                        foreach (FamilyIndividual indi in list)
                        {
                            writer.RenderBeginTag(HtmlTextWriterTag.Li);
                                WriteIndividualLink(writer, namer, indi);
                            writer.RenderEndTag();
                        }

                    writer.RenderEndTag();
                writer.RenderEndTag();
            writer.RenderEndTag();

        }

        protected static void WriteMenu(HtmlTextWriter writer, FileNameBuilder namer, FamilyInfo info)
        {

            writer.RenderBeginTag(HtmlTextWriterTag.Hr);
            writer.RenderEndTag();

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabber");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            CreateHomeTab( writer, namer, info );
            CreateIndividualsTab(writer, namer, info);
            CreateSourceTab(writer, namer, info);
        }



        protected static void WriteTitle(HtmlTextWriter writer, string Title)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "Title");
            writer.RenderBeginTag(HtmlTextWriterTag.H1);
            writer.Write(Title);
            writer.RenderEndTag();
        }

        protected static void WriteName(HtmlTextWriter writer, PersonalName name)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "given");
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write(name.GivenNames);
            writer.RenderEndTag();

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "surname");
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            string surname = name.Surname ?? string.Empty;
            writer.Write(surname.ToUpper());
            writer.RenderEndTag();
        }


        protected static string GetIndividualLink(FileNameBuilder namer, FamilyIndividual indi)
        {
            try
            {
                if (indi.Name == null)
                {
                    return "Unknown";
                }
                else
                {
                    string surname = indi.Name.Surname ?? String.Empty;
                    return string.Format("<a href=\"{0}\"><span class=\"given\">{1}</span> <span class=\"surname\">{2}</span></a>",
                        namer.GetPageName(indi).Name, indi.Name.GivenNames, surname.ToUpper());
                }
            }
            catch ( Exception )
            {

                return "Unknown";
            }

        }


        protected static void WriteIndividualLink(HtmlTextWriter writer, FileNameBuilder namer, FamilyIndividual indi)
        {
            if (indi != null)
            {
                writer.Write(GetIndividualLink(namer, indi));
            }
            else
            {
                writer.Write("Unknown");
            }
        }


        protected static string GetSourceLink(FileNameBuilder namer, FamilySource source)
        {
            return string.Format("<a href=\"{0}\">{1}</a>",
                namer.GetPageName(source).Name, source.Title);
        }


        protected static void WriteSourceLink(HtmlTextWriter writer, FileNameBuilder namer, FamilySource source)
        {
            if (source != null)
            {
                writer.Write(GetSourceLink(namer, source));
            }
            else
            {
                writer.Write("Unknown");
            }
        }



        protected static void WritePageLink(HtmlTextWriter writer, FileNameBuilder namer, FileNameBuilder.Page page)
        {
            WritePageLink(writer, namer, page, null);
        }


        protected static void WritePageLink(HtmlTextWriter writer, FileNameBuilder namer, FileNameBuilder.Page page, string cssClass)
        {
            if (!string.IsNullOrEmpty(cssClass))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "navlink");
            }

            writer.AddAttribute(HtmlTextWriterAttribute.Href, namer.GetPageName(page).Name);
            writer.RenderBeginTag(HtmlTextWriterTag.A);

            string name = "UnknownPage";
            switch (page)
            {
                case FileNameBuilder.Page.HomePage:
                    name = "Home";
                    break;

                case FileNameBuilder.Page.IndividualIndex:
                    name = "Individuals";
                    break;

                case FileNameBuilder.Page.SourceIndex:
                    name = "Sources";
                    break;
            }

            writer.Write(name);
            writer.RenderEndTag();
        }

    }
}
